package com.xdf.whiteaccount.service.impl;

import com.xdf.whiteaccount.entity.ContactCompany;
import com.xdf.whiteaccount.service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
import org.springframework.context.annotation.Lazy;
import com.xdf.whiteaccount.dao.AddressMapper;
import com.xdf.whiteaccount.entity.Address;
import com.xdf.whiteaccount.utils.BusinessException;
import com.xdf.whiteaccount.utils.Example;
import com.xdf.whiteaccount.utils.BeanUtils;
import com.xdf.whiteaccount.enums.OpeEnum;
import com.xdf.whiteaccount.enums.MoveBillFlag;

/**
 * 版本号：20240401
 * @Description : 服务层实现类，由代码生成器自动生成
 * @Author : chanchaw
 * @Date : 2025-5-15 9:19:37
 */
@Slf4j
@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper dao;

    @Override
    @Transactional
    public Address insert(Address record) {
        record = fullFill(record);
        doBeforeInsert(record);
        dao.insert(record);
        doAfterInsert(record);
        return record;
    }

    @Override
    @Transactional
    public Address insertSelective(Address record) {
        record = fullFill(record);
        doBeforeInsert(record);
        dao.insertSelective(record);
        doAfterInsert(record);
        return record;
    }

    /* 由于本方法无法返回所有对象的自增主键，后者调整为一个一个保存的方法
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int multiInsert(List<Address> list) {
        return dao.multiInsert(list);
    }
     */
    @Override
    @Transactional
    public List<Address> multiInsert(List<Address> list) {
        list.forEach(item -> insertSelective(item));
        return list;
    }

    @Override
    public int updateByPrimaryKey(Address record) {
        try {
            BeanUtils.fullFillRecord(record, OpeEnum.UPDATE);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return dao.updateByPrimaryKey(record);
    }

    @Override
    public int updateByPrimaryKeySelective(Address record) {
        try {
            BeanUtils.fullFillRecord(record, OpeEnum.UPDATE);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return dao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        Address dbRecord = dao.selectByPrimaryKey(id);
        if( dbRecord == null ) return 0;
        return dao.updateByPrimaryKey(dbRecord);
    }

    @Override
    public Address selectByPrimaryKey(Integer id) {
        return dao.selectByPrimaryKey(id);
    }

    // 根据主键作废
    @Override
    public int setVoid(Integer id){
        Address dbRecord = dao.selectByPrimaryKey(id);
        if( dbRecord == null ) return 0;
        return dao.updateByPrimaryKey(dbRecord);
    }
    @Override
    public List<Address> selectByParentId(Integer parentId){
        return dao.selectByExample(new Example().andEq("parent_id", parentId));
    }

    @Override
    public List<Address> selectAll(){
        return dao.selectAll();
    }

    @Override
    public Address save(Address record){
        if(record == null) throw new BusinessException("空对象不可保存！");
        Integer iid = Optional.ofNullable(record.getId()).orElse(0);
        if(iid==0) record = insertSelective(record);
        else updateByPrimaryKeySelective(record);
        return record;
    }

    // 没有使用 xml 中的 multiSave 方法是因为无法批量回写自增主键
    @Transactional
    @Override
    public List<Address> multiSave(List<Address> list){
        if(list == null || list.size()==0) return list;
        list.forEach(item -> item = save(item));
        return list;
    }

    /**=========================下面开始是固定模式代码=======================================*/
    // 新增前检测，不可新增则抛出异常
    private void check4Insert(Address record){
    }

    @Override
    public Address fullFill(Address record) {
        // TODO: 默认前端传递来的参数不需要填充完整，如果需要则使用自定义逻辑覆盖下面的代码
        try {
            // 根据“新增”或者“修改”填充创建人或者最后一次修改人
            // 要求表格中有字段：create_user,last_user
            BeanUtils.fullFillRecord(record,OpeEnum.NEW);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return record;
    }

    @Override
    public void doBeforeInsert(Address record) {
        // TODO: 在新增数据前需要做的逻辑写在这里，抛出异常则终止新增操作
        AddressService.super.doBeforeInsert(record);
    }

    @Override
    public void doAfterInsert(Address record) {
        // TODO: 新增数据后需要做的逻辑写在这里，抛出异常则终止新增操作
        AddressService.super.doAfterInsert(record);
    }

    @Override
    public List<Address> selectByExample(Example<Address> example){
        return dao.selectByExample(example);
    }

    @Override
    public Address moveBill(Integer id, String moveBillFlag){
        if(moveBillFlag.equalsIgnoreCase(MoveBillFlag.FIRST.getFlag())){
            Address first = dao.getFirst();
            if(first == null) throw new BusinessException("没有单据！");
            else return first;
        }

        if(moveBillFlag.equalsIgnoreCase(MoveBillFlag.LAST.getFlag())){
            Address last = dao.getLast();
            if(last == null) throw new BusinessException("没有单据！");
            else return last;
        }
        if(moveBillFlag.equalsIgnoreCase(MoveBillFlag.NEXT.getFlag())){
            Address next = dao.getNext(id);
            if(next == null) throw new BusinessException("没有下一单了！");
            else return next;
        }

        if(moveBillFlag.equalsIgnoreCase(MoveBillFlag.PREVIOUS.getFlag())){
            Address prev = dao.getPrev(id);
            if(prev == null) throw new BusinessException("没有上一单了！");
            else return prev;
        }

        return null;
        }
    //=================以上是自动生成的代码，自定义代码请写在下面==================

    /**
     * @Describe 根据Entity查询
     * @author 张柯
     * @Date 2021-05-16 09:40:12
     */
    @Override
    public List<Address> listQuery(Address record) throws Exception{
        return dao.selectByParam(record);
    }
}